package com.example.demo.controller;
import com.example.demo.entity.emp;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@ComponentScan
@RestController
@RequestMapping("/emp")
public class helloController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * @return
     * 查询全部信息
     */
    @RequestMapping("/list")
    public List<Map<String, Object>> empList() {
        String sql = "select * from emp";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        ModelAndView mav = new ModelAndView("aa");
        return mav;
    }

    @RequestMapping("/toDel")
    public ModelAndView toDel() {
        ModelAndView mav = new ModelAndView("delete");
        return mav;
    }
    @RequestMapping("/toUpd")
    public ModelAndView toUpd() {
        ModelAndView mav = new ModelAndView("update");
        return mav;
    }
    /**
     * 新增数据
     */
    @RequestMapping("/add")
    public @ResponseBody String  addItems(emp emp) {
        String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) value (?,?,?,?,?,?,?,?)";
        Object args[] = {emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "新增成功";
        }
        return "新增出现错误";
    }

    /**
     * 删除数据
     */
    @RequestMapping("/del")
    public @ResponseBody String  delItems(emp emp) {
        String sql = "delete from emp where empno = ?";
        Object args[] = {emp.getEmpno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "删除成功";
        }
        return "删除出现错误";
    }

    /**
     * 更新操作
     */
    @RequestMapping("/upd")
    public @ResponseBody String  updItems(emp emp) {
        String sql = "update emp set ename = ?,job = ?,mgr = ?,hiredate = ?,sal = ?,comm = ?,deptno = ? where empno = ?";
        Object args[] = {emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno(),emp.getEmpno()};
        int temp = jdbcTemplate.update(sql, args);
        if(temp > 0) {
            return "修改成功";
        }
        return "修改出现错误";
    }
}

